package com.bnz.service.impl;

import com.bnz.mapper.ClassesMapper;
import com.bnz.mapper.StudentMapper;
import com.bnz.pojo.Classes;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.Student;
import com.bnz.pojo.StudentVo;
import com.bnz.service.StudentService;
import com.bnz.util.MyBatisUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class StudentServiceImpl implements StudentService {
    //1. 查询所有学生
    @Override
    public List<Student> findAll() {
        try (SqlSession session = MyBatisUtil.getSqlSession()) {
            //1. 取得StudentMapper
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            //2. 查询所有学生
            List<Student> students = mapper.findAll();
            //3. 返回
            return students;
        }
    }

    //2. 分页查询
    @Override
    public PageResult<Student> findByPage(int page, int pagesize) {
        //2.1 开始分页
        PageHelper.startPage(page,pagesize);
        //2.2 使用sqlsession
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            //得到数据
            List<Student> all = mapper.findAll();
            //转换为page对象
            Page<Student> students = (Page<Student>) all;
            //返回
            return new PageResult<>(students.getPages(), students.getResult());
        }
//        try (SqlSession session = MyBatisUtil.getSqlSession()) {
//            //1. 取得StudentMapper
//            StudentMapper mapper = session.getMapper(StudentMapper.class);
//            ClassesMapper classesMapper = session.getMapper(ClassesMapper.class);
//            //2. 查询所有学生
//            List<Student> students = mapper.findAll();
//            //2.1 将学生与班级名称绑定
//            for (Student student : students) {
//                Classes classes = classesMapper.findById(student.getCid());
//                student.setCname(classes.getCname());
//            }
//            //2.3 转换为Page<Student> 对象
//            Page<Student> studentPage = (Page<Student>) students;
            //2.4 返回

    }
    /**
     * 3. 开始条件查询带分页功能
     * @param page
     * @param pagesize
     * @param studentVo
     * @return
     */
    @Override
    public PageResult<Student> search(int page, int pagesize, StudentVo studentVo) {
        //分页开始
        PageHelper.startPage(page,pagesize);
        //查数据
       try( SqlSession sqlSession = MyBatisUtil.getSqlSession()){
           StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
           ClassesMapper classesMapper = sqlSession.getMapper(ClassesMapper.class);
           List<Student> students = mapper.search(studentVo);
           //绑定班级名
           for (Student student : students) {
               Classes classes = classesMapper.findById(student.getCid());
               student.setCname(classes.getCname());
           }
           //转换page对象
           Page<Student> studentPage = (Page<Student>) students;
           //返回
           return new PageResult<>(studentPage.getPages(),studentPage.getResult());
       }
//
    }
}
